package bjfu.it.sun.graduationproject.voicechat;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import bjfu.it.sun.graduationproject.R;

/**
 * Dialog管理类
 */

public class DialogManager {
    private Dialog mDialog;
    private ImageView mIcon;
    private ImageView mVoice;
    private TextView mLabel;
    private Context mContext;
    private boolean wantCancel = false;

    public DialogManager(Context context) {
        mContext = context;
    }

    /**
     * 显示Dialog
     */
    public void showRecordingDialog() {
        //将布局应用于Dialog
        mDialog = new Dialog(mContext);
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View view = inflater.inflate(R.layout.dialog_recorder, null);
        mDialog.setContentView(view);
        //成员控件赋值
        mIcon = (ImageView) mDialog.findViewById(R.id.recorder_dialog_icon);
        mLabel = (TextView) mDialog.findViewById(R.id.recorder_dialog_label);
        mDialog.show();
    }

    public void recording() {
        if (mDialog != null && mDialog.isShowing()) {
            mIcon.setVisibility(View.VISIBLE);
            mLabel.setVisibility(View.VISIBLE);
            mIcon.setImageResource(R.drawable.recorder1);
            mLabel.setText("手指上滑，取消发送");
        }
    }

    public void wantToCancel() {
        wantCancel = true;
        if (mDialog != null && mDialog.isShowing()) {
            mIcon.setVisibility(View.VISIBLE);
            mLabel.setVisibility(View.VISIBLE);
            mIcon.setImageResource(R.drawable.cancel);
            mLabel.setText("松开手指，取消发送");
        }
    }

    public void tooShort() {
        if (mDialog != null && mDialog.isShowing()) {
            mIcon.setVisibility(View.VISIBLE);
            mLabel.setVisibility(View.VISIBLE);
            mIcon.setImageResource(R.drawable.voice_to_short);
            mLabel.setText("录音时间过短");
        }
    }

    public void dismissDialog() {
        if (mDialog != null && mDialog.isShowing()) {
            mDialog.dismiss();
            mDialog = null;
        }
    }

    /**
     * 通过level更新音量资源图片
     */
    public void updateVoiceLevel(int level) {
        if (mDialog != null && mDialog.isShowing() && !wantCancel) {
            int resId = mContext.getResources().getIdentifier("recorder" + level, "drawable", mContext.getPackageName());
            mIcon.setImageResource(resId);
        }
    }
}
